//
// Created by 32827 on 2024/1/20.
//
#include "yzl_ui.h"

YZ_COLOR YZL_UI_Background=YZ_COLOR_Value(0xFFFFFF);
YZF_BOOL YZL_UI_IS_INIT=YZF_FALSE;

YZF_BOOL yzl_ui_point_check(YZL_WIDGET *pWidget,uint16_t x,uint16_t y){
    if(pWidget->x <= x && x <= pWidget->x + pWidget->width && pWidget->y <= y && y <= pWidget->y + pWidget->height){
        return YZF_TRUE;
    }else{
        return YZF_FALSE;
    }
}
//TODO: 完善图片缓存机制
uint8_t imgTemp[2048];
FIL imgFile;
YZF_BOOL yzl_ui_draw_img(char *path,uint16_t x,uint16_t y,uint16_t width,uint16_t height){
    FRESULT retSD = f_open(&imgFile,(const char*)path,FA_READ|FA_OPEN_EXISTING);
    if(retSD!=FR_OK){
        yz_error("File Open Error: %d\n",retSD);
    }else{
        yz_info("Name %s Picture Size:%d KB\n",path,f_size(&imgFile)/1024);
        for(uint16_t i=0;i<height;i++) {
            UINT f_size = 0;
            retSD = f_read(&imgFile, (void *) imgTemp, width*2, &f_size);//读一行
            imgTemp[f_size] = 0;
            if (retSD != FR_OK) {
                yz_error("File Read Error: %d\n", retSD);
                break;
            } else {
                yzl_ui_draw_pixels(x, y + i, width, 1, (YZ_COLOR *) imgTemp);
            }
        }
    }
    f_close(&imgFile);
    return YZF_TRUE;
}
YZ_IMAGE_565A_Header image565AHeader;
YZF_BOOL yzl_ui_draw_565a(char *path,uint16_t x,uint16_t y){
    FRESULT retSD = f_open(&imgFile,(const char*)path,FA_READ|FA_OPEN_EXISTING);
    if(retSD!=FR_OK){
        yz_error("File Open Error: %d\n",retSD);
    }else{
        retSD=f_read(&imgFile, (void *) &image565AHeader, sizeof(image565AHeader), NULL);
        if (retSD != FR_OK) {
            yz_error("File Read Error: %d\n", retSD);
        } else {
            UINT f_size = 0;
            for(uint32_t i=0;i<image565AHeader.height;i++){
                retSD = f_read(&imgFile, (void *) imgTemp, image565AHeader.width*2, &f_size);//读一行
                if (retSD != FR_OK) {
                    yz_error("File Read Error: %d\n", retSD);
                    break;
                } else {
                    yzl_ui_draw_pixels_ex(x,y + i, image565AHeader.width, 1, (YZ_COLOR *) imgTemp,*((YZ_COLOR *)&image565AHeader.alphaColor));
                }
            }
        }
    }
    return YZF_TRUE;
}